/*
 * @lc app=leetcode.cn id=1807 lang=typescript
 *
 * [1807] 替换字符串中的括号内容
 */

// @lc code=start
function evaluate(s: string, knowledge: string[][]): string {
    const map = new Map();
    for (let i = 0; i < knowledge.length; i++) {
        map.set(`(${knowledge[i][0]})`, knowledge[i][1]);
    }

    let key = false;
    let str = "";
    let res = "";
    for (let i = 0; i < s.length; i++) {
        if (s[i] === "(") {
            key = true;
        }
        if (key) {
            str += s[i];
        } else {
            res += s[i];
        }

        if (s[i] === ")") {
            key = false;
            let v = map.get(str);
            res += v ? v : "?";
            str = '';
        }
    }
    return res;
}

// @lc code=end
